package leetcode.code1927;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public boolean sumGame(String num) {
		char[] cs = num.toCharArray();
		int len = cs.length;
		int lw = 0, lsum = 0, rw = 0, rsum = 0;
		for (int i = 0, j = len >> 1; i < len >> 1; i++, j++) {
			char l = cs[i];
			char r = cs[j];
			if (l == '?') {
				lw++;
			} else {
				lsum += l - '0';
			}
			if (r == '?') {
				rw++;
			} else {
				rsum += r - '0';
			}
		}
		int restw = 0, restsum = 0;
		if (lw == rw) {
			return rsum != lsum;
		} else if (lw > rw) {
			restw = lw - rw;
			if (lsum >= rsum) {
				return true;
			}
			restsum = rsum - lsum;
		} else {
			restw = rw - lw;
			if (rsum >= lsum) {
				return true;
			}
			restsum = lsum - rsum;
		}
		if ((restw & 1) == 1) {
			return true;
		}
		return restsum != (restw >> 1) * 9;
	}

	@Override
	public void debug4() {
		H.compare(false, this.sumGame("5023"));
		H.compare(false, this.sumGame("?3295???"));
		H.compare(true, this.sumGame("25??"));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
